package com.originskyseed.platform.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.service.ICmsRelateIsReadService;
import com.originskyseed.platform.cms.service.ICmsRelateViewService;
import com.originskyseed.platform.cms.service.ICmsService;
import com.originskyseed.platform.cms.service.ICmsTypeService;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.course.service.ICourseRelateIsReadService;
import com.originskyseed.platform.pk.service.IPkService;
import com.originskyseed.platform.task.service.ITaskRelateAssignPersonService;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IDeptService;
import com.originskyseed.platform.user.service.IParameterConfigureService;
import com.originskyseed.platform.user.service.IUserInfoService;

/**
 * 新增未读信息统计
 *
 * @author wlj
 */
@Controller
@RequestMapping("/cms")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsUnreadController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsUnreadController.class);

    @DubboReference
    private ICmsService cmsService;

    @DubboReference
    private IDeptService deptService;

    @DubboReference
    private ICmsTypeService cmsTypeService;

    @DubboReference
    private ICmsRelateViewService cmsRelateViewService;

    @DubboReference
    private IUserInfoService userInfoService;

    @DubboReference
    private ICmsRelateIsReadService cmsRelateIsReadService;

    @DubboReference
    private IParameterConfigureService parameterConfigureService;

    @DubboReference
    private ITaskRelateAssignPersonService taskRelateAssignPersonService;

    @DubboReference
    private IPkService pkService;

    @DubboReference
    private ICourseRelateIsReadService courseRelateIsReadService;

    /**
     * 未读信息数量及标识
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/cmsUnreadNum.json")
    @RequiredParams(names = {"type"})
    public BaseResponseBody cmsUnreadNum(HttpServletRequest request) {
        BaseResponseBody result = null;
        try {
            // 请求参数
            Integer type = this.getParameterInteger("type", null);//1任务 2新闻列表 3通知公告列表 4PK 5培训
            UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
            Map<String, Object> resultMap = new HashMap<>();
            if (userInfo == null)
            {
                return   new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), resultMap);
            }
            String userId = this.getParameterString("userId",null);
            if(userInfo!=null&&StringUtils.isBlank(userId)){
                userId = userInfo.getId();
            }
            Integer number = 0;
            Map<String, Object> map = new HashMap<>();

            map.put("objId",userId);
            map.put("isRead",2);
            if(type == 1){ //任务
                number = this.taskRelateAssignPersonService.countTaskIsRead(userId);
            }else if(type == 2){ //新闻列表
                map.put("type",1);
                number = this.cmsService.countNewsReadObjNum(map);
            }else if(type == 3){ //通知公告列表
                map.put("type",2);
                number = this.cmsService.countCmsReadObjNum(map);
            }else if(type == 4){ //pk
                //if(userInfo.getType() != 6 || userInfo.getType() != 11){
                //    userId = userInfo.getDeptId() == null?"":userInfo.getDeptId();
                //}
                number = this.pkService.countPkNum(userId);
            }else if(type == 5){ //课程
                number = this.courseRelateIsReadService.countIsReadNum(userId);
            }else if(type == 0){ //全返回
                resultMap.put("TasksCenter",this.taskRelateAssignPersonService.countTaskIsRead(userId));
                map.put("type",1);
                resultMap.put("NewsCenter",this.cmsService.countNewsReadObjNum(map));
                map.put("type",2);
                resultMap.put("Notificationlist",this.cmsService.countCmsReadObjNum(map));
                resultMap.put("LearnMyselfOpen",this.courseRelateIsReadService.countIsReadNum(userId));
                if(userInfo.getType() != 6 && userInfo.getType() != 11){
                    userId = userInfo.getDeptId() == null?"":userInfo.getDeptId();
                }
                resultMap.put("MyPkSquare",this.pkService.countPkNum(userId));
            }else {
                return  new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.TYPE_IS_NULL.getMsg()), null);
            }
            resultMap.put("number",number);
            result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), resultMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
        }
        return result;
    }

}
